package com.hylc.fengkong;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;

/**
 * @author lhx
 * @description:
 * @date : 2023/11/10 14:17
 */
public class InspectTheRiskControlEnvironmentUtils {
    private static InspectTheRiskControlEnvironmentUtils JiaoYanFengKongCanShuUtils;
    public static InspectTheRiskControlEnvironmentUtils getInstance() {
        if (JiaoYanFengKongCanShuUtils == null) {
            synchronized (InspectTheRiskControlEnvironmentUtils.class) {
                if (JiaoYanFengKongCanShuUtils == null) {
                    JiaoYanFengKongCanShuUtils = new InspectTheRiskControlEnvironmentUtils();
                }
            }
        }
        return JiaoYanFengKongCanShuUtils;
    }

    /**
     * 检验风控
     * @param activity
     * @param callBack
     */
    public void  Inspect(Activity activity,String RequestId,CallBack callBack){
        XXPermissions.with(activity)
                // 申请单个权限
                .permission(Permission.READ_PHONE_STATE)
                .permission(Permission.READ_MEDIA_IMAGES)
                .permission(Permission.READ_PHONE_NUMBERS)
                .permission(Permission.READ_MEDIA_AUDIO)
                .permission(Permission.READ_MEDIA_VIDEO)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            SysDataUtil.getInstance().getPictureSum(activity, new SysDataUtil.CallbackInt() {
                                @Override
                                public void callback(int num) {
                                    JSONObject params = null;
                                    try {
                                        params = SysDataUtil.getInstance().getLoginInfo2(activity);
                                        params.put("photos", num + "");
                                        params.put("appName", activity.getPackageName());
                                        params.put("source","sdk");
                                        params.put("deviceId",RequestId);
                                        JSONObject data= new JSONObject();
                                        data.put("mdKey",AESUtil.httpRequestProcess(params,"sdk"));
                                        data.put("apps",SysDataUtil.getInstance().getPkgList(activity));
                                        post(data.toString(),callBack);
                                    } catch (JSONException e) {
                                        throw new RuntimeException(e);
                                    }
                                }
                            });
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            XXPermissions.startPermissionActivity(activity, permissions);
                        } else {
                            Toast.makeText(activity, "请授予权限,并重新登陆", Toast.LENGTH_SHORT).show();
                        }
                    }
                });



    }


    private void post(String str,CallBack callBack){
           new Thread(new Runnable() {
                @Override
                public void run() {
                    URL url = null;
                        try {
                            url = new URL("http://47.94.192.132:9090/api/user/dwFkCheck");
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setDoOutput(true);
                            connection.setDoInput(true);
                            connection.setRequestProperty("User-Type","server");
                            connection.setRequestProperty("Accept","application/json");
                            connection.setRequestProperty("Content-Type", "application/json");
                            connection.setRequestMethod("POST");	//需要声明POST
                            OutputStream os = connection.getOutputStream();
                            DataOutputStream osw = new DataOutputStream(os);
                            osw.writeBytes(str);
                            Log.e("fengkong",str);
                            osw.flush();
                            osw.close();
                            connection.connect();
                            InputStream is = null;
                            if (connection.getResponseCode()==200){
                                is=connection.getInputStream();
                            }else {
                                is=connection.getErrorStream();
                            }
                            if (is==null) return;
                            InputStreamReader isr = new InputStreamReader(is, "UTF-8");	//GBK保证获取的汉字不乱码,utf-8会乱码
                            BufferedReader br = new BufferedReader(isr);
                            String line;
                            StringBuilder sb = new StringBuilder();
                            while ((line = br.readLine()) != null) {
                                sb.append(line);
                                String result = sb.toString(); //即请求得到的数据
                                Log.e("fengkong",result);
                            }
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    } catch (ProtocolException e) {
                        throw new RuntimeException(e);
                    } catch (UnsupportedEncodingException e) {
                        throw new RuntimeException(e);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
    }
    public interface  CallBack{
       void onRequestSuccess();
        void onRequestfail();
    }
}
